package com.cuitrip.presenter;

import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cuitrip.business.home.favorite.TravelTrip;
import com.cuitrip.business.home.favorite.proxy.FavProxy;
import com.cuitrip.business.home.favorite.ui.FavAnimation;
import com.cuitrip.business.tripservice.proxy.PriceProxy;
import com.cuitrip.component.Label;
import com.cuitrip.component.LabelView;
import com.cuitrip.service.R;
import com.lab.utils.ImageHelper;
import com.lab.utils.Utils;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.Bind;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by baziii on 15/8/10.
 */
public class RecommendItemViewHolder {

    @Bind(R.id.service_img)
    public ImageView serviceImg;
    @Bind(R.id.tagLayout)
    public LinearLayout tagLayout;
    @Bind(R.id.service_name)
    public TextView serviceName;
    @Bind(R.id.author_info)
    public TextView authorInfo;
    @Bind(R.id.service_info)
    public TextView serviceInfo;
    @Bind(R.id.author_img)
    public CircleImageView authorImg;
    @Bind(R.id.like_img)
    public ImageView likeImg;

    private View.OnClickListener mOnclickListener;

    public RecommendItemViewHolder(View view, View.OnClickListener onClickListener) {
        this.mOnclickListener = onClickListener;
        ButterKnife.bind(this, view);
    }

    public void render(final TravelTrip item) {
        ImageHelper.displayPersonImage(item.getHeadPic(), authorImg, null);
        ImageHelper.displayCtImage(item.getServicePicUrl(), serviceImg, ImageHelper.getCardFourThreeImageOptions());
        serviceName.setText(item.getServiceName());
        authorInfo.setText(Utils.mergeStrings(new String[]{item.getUserNick(),item.getCareer()},"·"));
        String price = PriceProxy.getInstance().priceFormatText(item.getPriceType(), item.getShowCurrency(), item.getShowPrice());
        String time = null;
        int hours = item.getServiceTime();
        if (hours>0){
            time = serviceInfo.getResources()
                    .getString(
                            hours == 1 ? R.string.trip_attribute_duration_value_one : R.string.trip_attribute_duration_value_more
                            ,hours);
        }
        String serviceInfoString = Utils.mergeStrings(new String[]{price,time,item.getServiceAddress()},"·");
        SpannableStringBuilder ssb = new SpannableStringBuilder(serviceInfoString);
        ssb.setSpan(new ForegroundColorSpan(serviceInfo.getResources().getColor(R.color.orange_ff6855)),0,price.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        serviceInfo.setText(ssb);
        tagLayout.removeAllViews();
//            List<Label> sources = new ArrayList<>();
//            sources.add(new Label(2,1,"00bce2","00d4ff","text1","ffffff",11,4,2));
//            sources.add(new Label(2,1,"00bce2","00d4ff","text2","ffffff",12,4,2));
//            sources.add(new Label(2,1,"00bce2","00d4ff","text3","ffffff",12,4,2));
        List<Label> labels = item.getLabelList();
        for(int i = 0; labels!=null && i<labels.size();i++) {
            LabelView labelView = new LabelView(tagLayout.getContext());
            Label label = labels.get(i);
            labelView.setLabel(label);
            LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            ll.rightMargin = Utils.dp2pixel(8);
            labelView.setLayoutParams(ll);
            tagLayout.addView(labelView);
        }
        FavProxy.getInstance().updateFavIcon(likeImg, true);
        likeImg.setTag(item);
        likeImg.setOnClickListener(mOnclickListener);
    }
}
